//
//  PlayeViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "PlayeViewController.h"

#import <AVFoundation/AVFoundation.h>
#import "playerView.h"


@interface PlayeViewController ()

//@property(nonatomic, strong)AVPlayer *videoPlayer;
//@property(nonatomic, strong)AVPlayerLayer *layer;

@property(nonatomic, retain)AVPlayer *player;
@property(nonatomic, retain)AVPlayerItem *playerItem;
@property(nonatomic, retain)playerView *playerView;
@property(nonatomic, retain)UIButton *stateButton;
@property(nonatomic, retain)UILabel *timeLabel;
@property(nonatomic, retain)id playbackTimeObserver;
@property(nonatomic, retain)UIButton *videoSlider;
@property(nonatomic, retain)UIButton *videoProgress;

@property(nonatomic, strong)UIView *myView;

@end

@implementation PlayeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor blackColor];
    //[self creatBo];
    
    
    NSURL *url = [NSURL URLWithString:_model.mp4_url];
    self.playerItem = [AVPlayerItem playerItemWithURL:url];
    [self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];// 监听status属性
    [self.playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];// 监听loadedTimeRanges属性
    self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
    self.playerView.player = _player;
    
    
    self.stateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.stateButton.backgroundColor = [UIColor whiteColor];

    
    
    self.stateButton.enabled = NO;
    
    
    
    

    // Do any additional setup after loading the view.
}























-(void)creatBo{
   /*
   // NSURL *url = [NSURL URLWithString:_model.mp4_url];
    
    //1.使用自定义播放器KrVideoPlayerController
   // [self addVideoPlayerWithURL:url];
    
    
//    //2.使用AV播放器
//    //构造AVPlayerItem对象
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//    //初始化player
//    self.videoPlayer = [[AVPlayer alloc] initWithPlayerItem:item];
//    //到这里还没有画面,我们还需要一个AVPlayerLayer对象
//    self.layer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
//    self.layer.frame = CGRectMake(0, 100, self.view.frame.size.width, 500);
//    self.layer.videoGravity = AVLayerVideoGravityResizeAspect;//让其居中显示
//    //添加到屏幕上显示
//    [self.view.layer addSublayer:self.layer];
//    
//    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 500)];
//
//    [self.view addSubview:self.myView];
//    
//    
//     [self.videoPlayer play];
    */
    
//    //返回按钮
//    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    //leftButton.backgroundColor = [UIColor whiteColor];
//    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
//    [self.videoController.view addSubview:leftButton];
//    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(0);
//        make.left.mas_equalTo(20);
//        
//    }];
//    [leftButton addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    
   
}

-(void)leftAction{
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}




-(void)setModel:(videoModel *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    _model = model;
//    self.str = _model.mp4_url;
}
















//1.使用自定义播放器KrVideoPlayerController
//- (void)addVideoPlayerWithURL:(NSURL *)url{
//    if (!self.videoController) {
//        CGFloat width = [UIScreen mainScreen].bounds.size.width;
//        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 64, width, width*(9.0/16.0))];
//        __block typeof(self)weakSelf = self;
//        [self.videoController setDimissCompleteBlock:^{
//            weakSelf.videoController = nil;
//        }];
////        [self.videoController setWillBackOrientationPortrait:^{
////            [weakSelf toolbarHidden:NO];
////        }];
////        [self.videoController setWillChangeToFullscreenMode:^{
////            [weakSelf toolbarHidden:YES];
////        }];
//        [self.view addSubview:self.videoController.view];
//    }
//    self.videoController.contentURL = url;
//
//}
////隐藏navigation tabbar 电池栏
//- (void)toolbarHidden:(BOOL)Bool{
//    self.navigationController.navigationBar.hidden = Bool;
//    self.tabBarController.tabBar.hidden = Bool;
//    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
//}
@end
